/****************************************
 * 2018 - 2021 版权所有 CopyRight(c) 快程乐码信息科技有限公司所有, 未经授权，不得复制、转发
 */

package com.kclm.security03.config.security.login;

import com.kclm.security03.config.security.dto.SecurityUser;
import com.kclm.security03.modules.common.dto.output.ApiResult;
import com.kclm.security03.modules.common.util.ResponseUtil;
import com.kclm.security03.modules.system.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/******************
 * @Author yejf
 * @Version v1.0
 * @Create 2020-08-05 21:55
 * @Description TODO
 */
@Slf4j
@Component
public class AdminAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    public AdminAuthenticationSuccessHandler() {
        log.debug("创建AdminAuthenticationSuccessHandler 对象");
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                            Authentication authentication) throws IOException, ServletException {
        //
        log.debug("认证成功：");
        log.debug("\t请求的url:"+request.getRequestURL());

        //通过 authentication来获取认证的凭证
        final SecurityUser principal = (SecurityUser) authentication.getPrincipal();
        log.debug("获取通过认证的用户SecurityUser："+principal);
        //创建User对象
        User user = new User();
        user.setToken(principal.getCurrentUserInfo().getToken());
        //输出
        ResponseUtil.out(response, ApiResult.ok("登录成功!", user));
    }


}
